/*
 * Copyright (C) 拾光分享网 
 * All rights reserved, Designed By www.hiai.top.
 * Email：goodsking@163.com.
 */
package top.hiai._admin.role;

import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import top.hiai._admin.permission.PermissionAdminService;
import top.hiai.common.model.Permission;
import top.hiai.common.model.Role;
import top.hiai.config.BaseController;

import java.util.LinkedHashMap;
import java.util.List;

/**
* @Author www.hiai.top
* @Email  goodsking@163.com
* @Message 更多资源尽在www.hiai.top
* @Introduction 类的介绍
*/

public class RoleAdminController extends BaseController {
	
	RoleAdminService srv = RoleAdminService.me;

	/**
	 * 角色列表
	 */
	public void index() {
		Page<Role> rolePage = srv.paginate(getParaToInt("p", 1),limtCount);
		setAttr("rolePage", rolePage);
		render("roleList.html");
	}
	/**
	 * 分配权限
	 */
	public void assignPermissions() {
		Role role = srv.findById(getParaToInt("id"));
		List<Permission> permissionList = PermissionAdminService.me.getAllPermissions();
		srv.markAssignedPermissions(role, permissionList);
		LinkedHashMap<String, List<Permission>> permissionMap = srv.groupByController(permissionList);

		setAttr("role", role);
		setAttr("permissionMap", permissionMap);
		render("assign_permissions.html");
	}
	
	/**
	 * 添加权限菜单
	 */
	public void addPermission() {
		Ret ret = srv.addPermission(getParaToInt("roleId"), getParaToInt("permissionId"));
		renderJson(ret);
	}

	/**
	 * 删除权限菜单
	 */
	public void deletePermission() {
		Ret ret = srv.deletePermission(getParaToInt("roleId"), getParaToInt("permissionId"));
		renderJson(ret);
	}
	
	/**
	 * 根据分类ID查找角色
	 * 用户角色的修改
	 */
	public void findById(){
		Role Role = srv.findById(getParaToInt("id"));
		renderJson("result",Role);
	}
	/**
	 * 添加角色
	 */
	public void roleAdd(){
		Boolean result = srv.roleAdd(get("name"));
		renderJson("success",result);
	}
	/**
	 * 修改角色
	 */
	public void roleEdit(){
		Boolean result = srv.roleEdit(getInt("id"),get("name"));
		renderJson("success",result);
	}
	/**
	 * 删除角色
	 */
	public void roleDelete(){
		srv.roleDelete(getInt("id"));
		redirect("/admin/role");
	}
}